package com.cn.service.server.PartsManagement.Impl;

import com.cn.bean.pojo.lbj.ParameterItemTable;
import com.cn.config.config.Result;
import com.cn.mapper.dao.db5.PartsManagement.ParameterItemTableDao;
import com.cn.service.server.PartsManagement.ParameterItemTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ParameterItemTableServiceImpl implements ParameterItemTableService {
    @Autowired(required = false)
    private ParameterItemTableDao parameterItemTableDao;
    @Override
    public List<ParameterItemTable> selectParameterItemTable(ParameterItemTable parameterItemTable) {
        return parameterItemTableDao.selectParameterItemTable(parameterItemTable);
    }

    @Override
    public List<ParameterItemTable> selectAll() {
        return parameterItemTableDao.selectAll();
    }

    @Override
    public Integer getParameterItemTableAutoid() {
        return parameterItemTableDao.getParameterItemTableAutoid();
    }

    @Override
    public Result addParameterItemTable(String itemName, String valueRangeType, String sourceMethod, String sourceTable, String sourcePrimaryKeyField, String standardValueField, String lowerLimitField, String upperLimitField, String description, String entryTimePoint, int isdel,int classificationId) {
        Integer ID= parameterItemTableDao.getParameterItemTableAutoid();
        ID = ID != null ? ++ID : 1;
        String itemCode = String.valueOf(ID);
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int i = parameterItemTableDao.addParameterItemTable(ID, itemCode, itemName, valueRangeType, sourceMethod,sourceTable,sourcePrimaryKeyField, standardValueField, lowerLimitField, upperLimitField,description,format.format(date),isdel,classificationId);
        if (i == 0) {
            return Result.failure("500", "添加失败");
        }
        return Result.success("200", "添加成功");
    }

    @Override
    public Result delParameterItemTableStatus(int id) {
        int i = parameterItemTableDao.delParameterItemTableStatus(id);
        if (i > 0) {
            return Result.success("200", "删除成功");
        }
        return Result.failure("500", "删除失败");
    }

    @Override
    public Result updateParameterItemTable(int ID, String itemCode, String itemName, String valueRangeType, String sourceMethod, String sourceTable, String sourcePrimaryKeyField, String standardValueField, String lowerLimitField, String upperLimitField, String description, String entryTimePoint, int isdel,int classificationId) {
        List<ParameterItemTable> businessTypeTables = parameterItemTableDao.selectAll();

        for (ParameterItemTable businessType : businessTypeTables) {
            if (businessType.getId() != ID && businessType.getItemName().equals(itemName)) {
                return Result.failure("500", "名称重复");
            }
        }
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int i = parameterItemTableDao.updateParameterItemTable(ID, itemCode, itemName, valueRangeType, sourceMethod,sourceTable,sourcePrimaryKeyField, standardValueField, lowerLimitField, upperLimitField,description,format.format(date),isdel,classificationId);

        if (i > 0) {
            return Result.success("200", "修改成功");
        }
        return Result.failure("500", "修改失败");
    }
}
